<template>
    <div>
        <h1>新增食谱</h1>
<table class="table">
    <tbody>
        <tr>
            <td>标题</td>
            <td><input type="text" v-model="Adddata.bitton"></td>
        </tr>
        <tr>
            <td>食品封面</td>
            <td>
                <input type="file" @change="Upfile" >
                <img style="width: 60px; height: 60px;":src="axios.defaults.baseURL+Adddata.img" alt="">
            </td>
        </tr>
        <tr>
            <td>分类</td>
            <td>
                <select v-model="Adddata.tid">
                    <option value="0">请选择</option>
                    <option v-for="d in Bind":value=d.tid>{{d.tName  }}</option>
                </select>
            </td>
        </tr>
      
        <tr>
            <td>分享</td>
            <td><input type="text" v-model="Adddata.share"></td>
        </tr>
        <tr>
            <td>收藏</td>
            <td><input type="text" v-model="Adddata.boon"></td>
        </tr>
          <tr>
            <td>标签</td>
            <td>
               <select v-model="Adddata.bting">
                
                <option value="低盐">低盐</option>
                <option value="低糖">低糖</option>
               </select>
            </td>
        </tr>
        <tr>
            <td>更新人</td>
            <td><input type="text" v-model="Adddata.name"></td>
        </tr>
        <tr>
            <td>状态</td>
            <td> 
                  <input type="radio" name="AAA" :value=true  v-model="Adddata.state">已发布
                  <input type="radio" name="AAA" :value=false v-model="Adddata.state">未发布
            </td>
        </tr>
         <tr>
            <td></td>
            <td><input type="button" value="保存" class="btn btn-success"  @click="Add"></td>
        </tr>
    </tbody>
</table>
    </div>
</template>

<script setup lang="ts">
import axios from 'axios'
import {ref,onMounted}from 'vue'
import { useRouter } from 'vue-router';

//跳转数组
const router=useRouter()

//上传文件
const Upfile=(abc:any)=>{
 let f=abc.target.files[0]
 let fd=new FormData()
 fd.append("file",f)
 axios.post("/api/Upload",fd).then(res=>{
    Adddata.value.img=res.data
 })
}



//绑定文档就绪函数
onMounted(()=>{
 BangType()
})
//绑定数据
const Bind=ref([{
      "tid": 0,
       "tName": ""
}])

const BangType=()=>{
    axios.get("/api/TypeBll/GetFoodTypes").then(res=>{
        Bind.value=res.data
    })
}


//添加数据
const Adddata=ref({
     "isdel": true,
  "createId": 0,
  "createtime": "2025-07-14T07:06:10.434Z",
  "deleteId": 0,
  "deletetime": "2025-07-14T07:06:10.434Z",
  "updateId": 0,
  "updatetime": "2025-07-14T07:06:10.434Z",
  "id": 0,
  "bitton": "",
  "img": "",
  "tid": 0,
  "bting": "",
  "share": "",
  "boon": "",
  "name": "",
  "state": true,
  "upTime": "2025-07-14T07:06:10.434Z"
})

//添加
const Add=()=>{
       if(Adddata.value.bitton=="")
    {
        alert("标题不能为空");
        return;
    }
     if(Adddata.value.img=="")
    {
        alert("请添加图片");
        return;
    }
     if(Adddata.value.bting=="")
    {
        alert("标签不能为空");
        return;
    }
     if(Adddata.value.tid==0)
    {
        alert("请选择分类");
        return;
    }

    
    axios.post("/api/Food/AddFood",Adddata.value).then(res=>{
        if(res.data>0)
        {
         alert("添加成功")   
        router.push("/Showfood")
        }
        else
        {
            alert("添加失败");
        }
    })
}



</script>

<style scoped>

</style>